﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.IO;
using Obra.Api;
using Obra.SmugMug.Api;

namespace Obra.Test.SmugMug
{
    [TestFixture]
    public class SmugMugUploader
    {
        private string uploadUri = Messages.TestUrl;

        [Test]
        public void UploadOneFile()
        {
            string data = "this-is-image-data";
            string sessionId = "c525168360b919d19135993990a992bd";
            string uploadContent = File.ReadAllText(@"..\..\Smugmug\sample\SmugMugUpload.xml");

            using (HttpServerQueuedContent httpServer = new HttpServerQueuedContent(new Uri(this.uploadUri)))
            using (Obra.SmugMug.Api.UploadServiceProvider uploader = new Obra.SmugMug.Api.UploadServiceProvider(
                sessionId,
                Messages.TestUrl))
            {
                httpServer.ContentQueue.Enqueue(uploadContent);
                httpServer.RequestValidateQueue.Enqueue(new SmugMugUserAgentValidate());

                SmugMugUploadRequestValidate validator = new SmugMugUploadRequestValidate()
                {
                    ExpectedAlbumId = "1000",
                    ExpectedChecksum = TestHelper.ComputeMD5ChecksumOfString(data),
                    ExpectedFileName = "image-that-does-not-already-exist.jpg",
                    ExpectedImageId = null,
                    ExpectedLength = (ulong)data.Length,
                    ExpectedResponseType = "REST",
                    ExpectedSessionId = "c525168360b919d19135993990a992bd",
                    ExpectedVersion = "1.2.0",
                };

                httpServer.RequestValidateQueue.Enqueue(validator);
                httpServer.Start();

                IAlbum album = new AlbumContract()
                {
                    Id = 1000,
                    Title = "Album",
                };

                uploader.UploadMediaFile(
                    album,
                    "image-that-does-not-already-exist.jpg",
                    TestHelper.GetStreamFromString("this-is-image-data"));
            }
        }

        [Test]
        public void UploadOneFileFail()
        {
            string data = "this-is-image-data";
            string sessionId = "c525168360b919d19135993990a992bd";
            string uploadContent = File.ReadAllText(@"..\..\Smugmug\sample\SmugMugUploadFail.xml");

            try
            {
                using (HttpServerQueuedContent httpServer = new HttpServerQueuedContent(new Uri(this.uploadUri)))
                using (Obra.SmugMug.Api.UploadServiceProvider uploader = new Obra.SmugMug.Api.UploadServiceProvider(
                    sessionId,
                    Messages.TestUrl))
                {
                    httpServer.ContentQueue.Enqueue(uploadContent);
                    httpServer.RequestValidateQueue.Enqueue(new SmugMugUserAgentValidate());

                    SmugMugUploadRequestValidate validator = new SmugMugUploadRequestValidate()
                    {
                        ExpectedAlbumId = "1000",
                        ExpectedChecksum = TestHelper.ComputeMD5ChecksumOfString(data),
                        ExpectedFileName = "image-that-does-not-already-exist.jpg",
                        ExpectedImageId = null,
                        ExpectedLength = (ulong)data.Length,
                        ExpectedResponseType = "REST",
                        ExpectedSessionId = "c525168360b919d19135993990a992bd",
                        ExpectedVersion = "1.2.0",
                    };

                    httpServer.RequestValidateQueue.Enqueue(validator);
                    httpServer.Start();

                    IAlbum album = new AlbumContract()
                    {
                        Id = 1000,
                        Title = "Album",
                    };

                    uploader.UploadMediaFile(
                        album,
                        "image-that-does-not-already-exist.jpg",
                        TestHelper.GetStreamFromString("this-is-image-data"));
                }

                Assert.Fail("Did not expect to execute this statement!");
            }
            catch (SmugMugException e)
            {
                StringAssert.StartsWith("wrong format", e.Message);
            }
        }
    }
}
